var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var pickerFile = require('../../wxParse/picker_datetime.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hunterInfo:{},
    hunterid:0,
    itemid:0,
    adviseProId:null,
    iteminfo:{},
    calendar: [],
    width: 0,
    currentIndex: 0,
    datelist:{},
    radioCheckVal:true,
    orderCheckVal:null,
    date:null,
    month:null,
    protimestart_ms:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    
    var that = this;
    var date = new Date();
    var month = date.getMonth() + 1;
    var dateStr = date.getFullYear() + "-" + "0"+month + "-" + p(date.getDate());
    console.log(dateStr);
    console.log(JSON.parse(options.hunter));
    that.setData({
      hunterInfo: JSON.parse(options.hunter),
      adviseProId: options.adviseProId,
    });
    wx.setNavigationBarTitle({
      title: that.data.hunterInfo.name + "顾问"//页面标题为路由参数
    })
    function p(s) {
      return s < 10 ? '0' + s : s;
    }
    //已选择的咨询项目
    var itemid = options.adviseProId;
    util.post(app.api.getOrderDescId, { oritemid: itemid }, function (res) {
      console.log("已选的咨询项目"+res.data);
      that.setData({
        iteminfo: res.data,
        resTotal: res.data.spec
      });
    });
    
    util.post(app.api.getOrderTime, { hunterid: that.data.hunterInfo.id }, function (res) {
      console.log("getyuyueData=" + res);
      that.setData({
        datelist:res.data,
        date: dateStr,
        month:month
      });
    });
  
  },

  
  //关闭成功提示
  closeTap: function () {
    this.setData({
      mode: true,
      height: "100%"
    })


  },

  submitOrder: function (e) {
    var that=this;
    var proId = that.data.adviseProId;
    var proType=that.data.radioCheckVal;
    var proTime = that.data.orderCheckVal;
    
    console.log(proType);
    console.log("预约时间之前" + proTime);
    console.log("预约时间" + that.data.protimestart_ms);
    function getHour(s1, s2) {
      s1 = new Date(s1.replace(/-/g, '/'));
      s2 = new Date(s2.replace(/-/g, '/'));
      var ms = Math.abs(s1.getTime() - s2.getTime());
      return ms / 1000 / 60 / 60;
    }
    function getNowFormatDate() {
      var date = new Date();
      var seperator1 = "-";
      var seperator2 = ":";
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
      return currentdate;
    }
    console.info("是否小于48小时时间" + getHour(getNowFormatDate(), that.data.protimestart_ms));
    if (!proType){
      app.alert("请选择咨询方式")
    } else if (proTime==null){
      app.alert("请选择您预约的时间")
    } else if (getHour(getNowFormatDate(), that.data.protimestart_ms)<48){
      app.alert("因顾问需对您的情况进行了解及分析，为了保证咨询质量，建议您提前48小时进行预约")
    }else{
      wx.navigateTo({
        url: '../hunter-order/hunter-order?hunter='
          + JSON.stringify(this.data.hunterInfo) + '&proId=' + proId + '&proType=' + proType + '&proTime=' + proTime
      });

    }
  
  },
  
  //咨询方式
  clicks: function (e) {
    // console.log("咨询方式"+e.detail.value);
    // if(!e.detail.value){
    //   this.setData({
    //     radioCheckVal: true
    //   })
    // }else{
    //   this.setData({
    //     radioCheckVal:false
    //   })
    // }
    this.setData({
      radioCheckVal: !this.data.radioCheckVal
    })
    
  },
  //咨询时间
  clickOrderTime:function(e){
    var that=this;
    console.log("咨询时间"+e.detail.value);
    util.post(app.api.getOrderTimeId, { proTimeId: e.detail.value }, function (res) {
      console.log("已选的咨询时间" + res.data);
      that.setData({
        proTimeInfo: res.data,
        protimestart_ms: res.data.date + " " + res.data.startTime+":00",
        orderCheckVal: e.detail.value,
      });
    });
  }

})